﻿using System;
using System.Drawing;
using ComponentFactory.Krypton.Toolkit;
using ExtensibleEventViewer.Core.Primitives;


namespace ExtensibleEventViewer.Console.Controls
{
	public partial class EventLogRecordDetailsControl : DecoratedUserControl
	{
		private EventLogRecord _eventLogRecordToDisplay;
		private Func<EventLogRecord, string> _descriptionProcessor;
		
		public EventLogRecordDetailsControl()
		{
			InitializeComponent();
		}

		public void ShowEventLogRecordDetails(
			string logName, 
			EventLogRecord eventLogRecordToDisplay, 
			Func<EventLogRecord, string> descriptionProcessor)
		{
			_eventLogRecordToDisplay = eventLogRecordToDisplay;
			_descriptionProcessor = descriptionProcessor;

			string processedText = DescriptionProcessor != null
			                           	?
			                           		DescriptionProcessor(eventLogRecordToDisplay)
			                           	:
			                           		eventLogRecordToDisplay.Description;
			
			txtEventDescription.Text = string.Empty;
			txtEventDescription.AppendText(processedText);

			lblEventType.Text = eventLogRecordToDisplay.Type.ToString();
			lblLogName.Text = logName;
			lblSource.Text = eventLogRecordToDisplay.Source;
			lblTimeGenerated.Text = eventLogRecordToDisplay.TimeGenerated.ToString();
			lblUser.Text = eventLogRecordToDisplay.UserName;
			lblComputerName.Text = eventLogRecordToDisplay.ComputerName;
		}

		private void btnCloseDetails_Click(object sender, EventArgs e)
		{
			if (groupDetails.Collapsed)
			{
				_panelSize = groupDetails.Size;
				groupDetails.Size = new Size(groupDetails.Size.Width, groupDetails.Size.Height - groupDetails.Panel.Size.Height);
			}
			else
			{
				groupDetails.Size = _panelSize;
			}
		}

		private Size _panelSize;

		public string EventLogRecordDescription
		{
			get
			{
				return txtEventDescription.Text;
			}
		}

		public Func<EventLogRecord, string> DescriptionProcessor
		{
			get
			{
				return _descriptionProcessor;
			}
			set
			{
				_descriptionProcessor = value;
			}
		}

		public EventLogRecord EventLogRecordToDisplay
		{
			get { return _eventLogRecordToDisplay; }
			set { _eventLogRecordToDisplay = value; }
		}

		public override PaletteMode PaletteMode
		{
			set
			{
				groupDetails.PaletteMode = groupDescription.PaletteMode = value;
			}
		}
	}
}
